package com.mjlf.boot.event;

import com.mjlf.boot.service.ScreenControlClient;
import com.mjlf.boot.thrift.idl.EventData;
import com.mjlf.boot.thrift.idl.EventType;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * @ClassName ClientMourseEvent
 * @Author mjlft
 * @Date 2020/3/6 19:38
 * @Version 1.0
 * @Description 鼠标键盘是事件
 */
public class ClientMourseEvent extends JPanel implements MouseWheelListener, MouseListener, MouseMotionListener, KeyListener {
    private static boolean draging = false;
    public void paint(Graphics g) {
        //覆盖父类的方法
        super.paint(g);
    }

    //鼠标单击
    public void mouseClicked(MouseEvent e) {

    }

    //鼠标按下
    public void mousePressed(MouseEvent e) {
        draging = false;
        doMouseEvent(e, EventType.MOUSE_PRESS);
    }

    //鼠标松开
    public void mouseReleased(MouseEvent e) {
        draging = false;
        doMouseEvent(e, EventType.MOUSE_RELEASE);
    }

    public void doMouseEvent(MouseEvent e, EventType type){
        EventData eventData = new EventData();
        eventData.setEventType(type);
        if(e.getButton() == 1){
            eventData.setMouseButtons(InputEvent.BUTTON1_DOWN_MASK);
        }else if(e.getButton() == 2){
            eventData.setMouseButtons(InputEvent.BUTTON2_DOWN_MASK);
        }else if(e.getButton() == 3){
            eventData.setMouseButtons(InputEvent.BUTTON3_DOWN_MASK);
        }else {
            eventData.setMouseButtons(0);
        }

        eventData.setPointX(e.getX());
        eventData.setPointY(e.getY());

        try {
            ScreenControlClient.sendEvent(eventData);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    //鼠标移动到界面中
    public void mouseEntered(MouseEvent e) {
    }

    //鼠标从界面离开
    public void mouseExited(MouseEvent e) {
    }

    //鼠标拖拽
    public void mouseDragged(MouseEvent e) {
        //robot没有拖动功能， 只能使用组合的方式实现这个功能
        if(draging){
            doMouseEvent(e, EventType.MOUSE_MOVE);
        }else {
            draging = true;
            doMouseEvent(e, EventType.MOUSE_PRESS);
        }
    }

    //鼠标移动
    public void mouseMoved(MouseEvent e) {
        draging = false;
       doMouseEvent(e, EventType.MOUSE_MOVE);
    }

    @Override
    public void mouseWheelMoved(MouseWheelEvent e) {
        draging = false;
        int button = e.getButton();
        EventData eventData = new EventData();
        eventData.setEventType(EventType.MOUSE_WHEEL);
        eventData.setPointX(e.getX());
        eventData.setPointY(e.getY());
        eventData.setWheelNum(e.getScrollAmount() * e.getWheelRotation());
        eventData.setMouseButtons(button);
        try {
            ScreenControlClient.sendEvent(eventData);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        draging = false;
        EventData eventData = new EventData();
        eventData.setEventType(EventType.KEY_PRESS);
        eventData.setKeyCode(e.getKeyCode());
        try {
            if(e.getKeyChar() == KeyEvent.VK_TAB){
                System.out.println("");
                transferFocus();
            }

            ScreenControlClient.sendEvent(eventData);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        draging = false;
        EventData eventData = new EventData();
        eventData.setEventType(EventType.KEY_RELEASE);
        eventData.setKeyCode(e.getKeyCode());
        try {
            ScreenControlClient.sendEvent(eventData);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
